//
//  KHCustomTool.m
//  KissHealth
//
//  Created by Mark on 2017/1/9.
//  Copyright © 2017年 Mark. All rights reserved.
//

#import "KHCustomTool.h"
#import "AppDelegate.h"
#import "Reachability.h"
#import "KHLoginVC.h"
#import "KHTabBarViewController.h"
#import "KHNavigationController.h"
#import "JPUSHService.h"
#import "KHCommentVC.h"
// 账号的存储路径
#define kPhotoChangePath [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"photoChange.archive"]

@implementation KHCustomTool



/**
 * 吐司显示
 * @param toast
 */
//{"toast":toast}
+ (void)showToast:(NSString *)toast{
    [SVProgressHUD showInfoWithStatus:toast];
}

/**
 * 缓存一对键值对到文件
 * @param key
 * @param value
 */
//{"key":key,"value":value}
+ (void)setValue:(NSString *)value key:(NSString *)key{
    
    [[NSUserDefaults standardUserDefaults] setObject:value forKey:key];
    [[NSUserDefaults standardUserDefaults] synchronize];

}
/**
 * 获取key对应的值
 * @param key
 * @return
 */
//{"key":key}
+ (NSString *)getValue:(NSString *)key{
    NSString *value = [[NSUserDefaults standardUserDefaults] objectForKey:key];
    return value;
}

/**
 * 移除key及其对应的值
 */
+ (void)removeValue:(NSString *)key{
    // 删除
    [[NSUserDefaults standardUserDefaults]removeObjectForKey:key];
}

/**
 * 获得设备唯一编码
 */
+ (NSString *)getDeviceUniqueId{
    return [[[UIDevice currentDevice] identifierForVendor] UUIDString];
}



/**
 * 显示主界面的Fragment的跳转
 *
 *  {"index":index,"param":param}
 * index 哪一个 param map类型json数据
 */
+ (void)showFragment:(NSInteger)index {
    //选中那一个
    AppDelegate *app = (AppDelegate *)[[UIApplication  sharedApplication] delegate];
    KHTabBarViewController *tabBarVC =(KHTabBarViewController *)app.window.rootViewController;
    
    //回到根控制器
    KHNavigationController *nav =(KHNavigationController *) tabBarVC.selectedViewController;
    UIViewController *vc = nav.visibleViewController;
    [vc.navigationController popToRootViewControllerAnimated:NO];
    
    //在选中
    tabBarVC.selectedIndex = index;

    
}

/**
 * 清空缓存
 */
+ (void)cleanCache{
    // 清空网页浏览数据缓存数据
    [[NSURLCache sharedURLCache] removeAllCachedResponses];    
    NSURLCache *sharedCache = [[NSURLCache alloc] initWithMemoryCapacity:0 diskCapacity:0 diskPath:nil];
    [NSURLCache setSharedURLCache:sharedCache];
}

/**
 * 获取极光推送pushID
 */
+ (NSString *)getPushId{
    __block NSString *registrationId = @"1";
    //建议使用此接口获取registrationID，模拟器中调用此接口resCode返回1011,registrationID返回nil.
    [JPUSHService registrationIDCompletionHandler:^(int resCode, NSString *registrationID) {
        registrationId = registrationID;
        NSLog(@"resCode : %d,registrationID: %@",resCode,registrationID);
    }];
    
    return registrationId;
}

/**
 * 返回版本号
 *
 */

+ (NSString *)getVersion{
    return [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
}

/**
 * 退出程序 进入登录界面
 */
+ (void)quitApp{
    
    KHLoginVC *loginVC = [[KHLoginVC alloc]initWithURL:kLoginURLString];
    AppDelegate *app = (AppDelegate *)[[UIApplication  sharedApplication] delegate];
    app.window.rootViewController = nil;
    app.window.rootViewController = loginVC;
    
}

+(void)startWebSite:(NSString *)urlStr{
    NSURL *URL = [NSURL URLWithString:[NSString stringWithFormat:@"%@", urlStr]];
    [[UIApplication sharedApplication] openURL:URL];
}


/**
 * 获得网络状态
 * @return 01 无网络
 *  0 有网络
 */
+ (NSInteger)getNetState{
    
    AppDelegate *app = (AppDelegate *)[[UIApplication  sharedApplication] delegate];
    if ([app.conn currentReachabilityStatus] == NotReachable) {
        
        return -1;

    } else if ([app.conn currentReachabilityStatus] == ReachableViaWiFi) {
        
        return 0;
        
    } else {
        
        return 0;
        
    }
}


//{
//    "content_type": 1,
//    "title": "string",
//    "message": "string",
//    "extras": {
//        "url":"需要打开的url",
//        "extras":"2017-01-10 05:31:19"
//    }

//跳到通用界面
+(void)handlePushMessage:(NSDictionary *)dic{
    NSDictionary *extrasDic = [dic objectForKey:@"extras"];
    NSString *urlStr = [extrasDic objectForKey:@"url"];
    if (urlStr == nil ||[urlStr isEqualToString:@""]) {
        return;
    }
    AppDelegate *app = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    KHTabBarViewController *tabBarVC =(KHTabBarViewController *)app.window.rootViewController;
    
    KHNavigationController *nav =(KHNavigationController *) tabBarVC.selectedViewController;
    UIViewController *vc = nav.visibleViewController;

    //跳到通用界面
    [vc.navigationController pushViewController:[[KHCommentVC alloc]initWithURL:urlStr] animated:YES];

}

+(void)finishActivity{
    AppDelegate *app = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    KHTabBarViewController *tabBarVC =(KHTabBarViewController *)app.window.rootViewController;
    if ([tabBarVC isKindOfClass:[KHLoginVC class]]) {
        return;
    }
    KHNavigationController *nav =(KHNavigationController *) tabBarVC.selectedViewController;
    UIViewController *vc = nav.visibleViewController;
    [vc.navigationController popViewControllerAnimated:YES];
    
}
@end

